package com.xlh.service.courseTest.impl;

import com.google.common.collect.Lists;
import com.xlh.dao.user.ClassCourseMapper;
import com.xlh.dao.user.UserClassMapper;
import com.xlh.exception.common.GlobalException;
import com.xlh.pojo.user.ClassCourse;
import com.xlh.pojo.user.ClassCourseExample;
import com.xlh.pojo.user.UserClass;
import com.xlh.pojo.user.UserClassExample;
import com.xlh.service.courseTest.UserClassService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class UserClassServiceImpl implements UserClassService {
    @Autowired
    private ClassCourseMapper classCourseMapper;
    @Autowired
    private UserClassMapper userClassMapper;

    @Override
    public Long getUserClassId(Long courseId, Long userId) {
        // 获取班级id
        List<UserClass> userClassList = getUserClassList(userId);
        List<Long> classIds = userClassList.stream().map(UserClass::getClassId).collect(Collectors.toList());

        List<ClassCourse> classCourseList = getClassCourseByClassIdsCourseId(classIds, courseId);
        if (CollectionUtils.isEmpty(classCourseList)) {
            return null;
        }

        if (classCourseList.size() != 1) {
            throw new GlobalException("异常的班级-课程绑定关系");
        }

        ClassCourse classCourse = classCourseList.get(0);
        Long classId = classCourse.getClassId();

        // 查询出对应的user_class_id
        for (UserClass userClass : userClassList) {
            if (Objects.equals(classId, userClass.getClassId())) {
                return userClass.getId();
            }
        }

        return null;
    }

    @Override
    public List<UserClass> getUserClassIds(Long classId, Long userId) {
        if (classId == null && userId == null) {
            return Lists.newArrayList();
        }

        UserClassExample example = new UserClassExample();
        UserClassExample.Criteria criteria = example.createCriteria().andDeletedEqualTo(false);
        if (classId != null) {
            criteria.andClassIdEqualTo(classId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<UserClass> list = userClassMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public List<UserClass> selectByIds(List<Long> userClassIds) {
        if(CollectionUtils.isEmpty(userClassIds)) {
            return Lists.newArrayList();
        }
        UserClassExample example = new UserClassExample();
        example.createCriteria().andDeletedEqualTo(false).andIdIn(userClassIds);
        List<UserClass> list = userClassMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public List<UserClass> getByClassIds(List<Long> classIds) {

        UserClassExample example = new UserClassExample();
        example.createCriteria().andClassIdIn(classIds).andDeletedEqualTo(false);
        List<UserClass> list = userClassMapper.selectByExample(example);

        return list;
    }

    /**
     * 使用班级ids和课程id过滤查询关联关系
     *
     * @param classIds 班级id集
     * @param courseId 课程id
     * @return 关联关系集
     */
    private List<ClassCourse> getClassCourseByClassIdsCourseId(List<Long> classIds, Long courseId) {
        if (CollectionUtils.isEmpty(classIds)) {
            return Lists.newArrayList();
        }
        ClassCourseExample example = new ClassCourseExample();
        example.createCriteria().andClassIdIn(classIds).andCourseIdEqualTo(courseId).andDeletedEqualTo(false);
        List<ClassCourse> list = classCourseMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    private List<UserClass> getUserClassList(Long userId) {
        UserClassExample example = new UserClassExample();
        example.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<UserClass> list = userClassMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }
}
